package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorPatientCheckInSettingsDTO;
import com.pshcp.domain.model.DoctorAssistantSettings;
import com.pshcp.domain.model.DoctorPatientCheckInSettings;
import com.pshcp.mapper.DoctorPatientCheckInSettingsMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 专家患者报道设置表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorPatientCheckInSettingsService {

    @Autowired
    private DoctorPatientCheckInSettingsMapper doctorPatientCheckInSettingsMapper;

    public DoctorPatientCheckInSettings getDoctorPatientCheckInSettingsByDoctorId(Long doctorId) {
        Example example = new Example(DoctorPatientCheckInSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorPatientCheckInSettings> list = doctorPatientCheckInSettingsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<DoctorPatientCheckInSettings> list(DoctorPatientCheckInSettingsDTO doctorPatientCheckInSettingsDTO) {
        if (doctorPatientCheckInSettingsDTO.getPageNum() != null && doctorPatientCheckInSettingsDTO.getPageSize() != null) {
            PageHelper.startPage(doctorPatientCheckInSettingsDTO.getPageNum(), doctorPatientCheckInSettingsDTO.getPageSize());
        }
        Example example = new Example(DoctorPatientCheckInSettings.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorPatientCheckInSettingsDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorPatientCheckInSettingsDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorPatientCheckInSettings> list = doctorPatientCheckInSettingsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorPatientCheckInSettings> select(DoctorPatientCheckInSettings doctorPatientCheckInSettings) {
        return doctorPatientCheckInSettingsMapper.select(doctorPatientCheckInSettings);
    }

    public int insert(DoctorPatientCheckInSettings doctorPatientCheckInSettings) {
        doctorPatientCheckInSettings.setId(SystemUtil.getId());
        doctorPatientCheckInSettings.setStatus(CommonStatusEnum.INIT.getCode());
        doctorPatientCheckInSettings.setCreatedTime(new Date());
        return doctorPatientCheckInSettingsMapper.insert(doctorPatientCheckInSettings);
    }

    public int update(DoctorPatientCheckInSettings doctorPatientCheckInSettings) {
        doctorPatientCheckInSettings.setUpdateTime(new Date());
        return doctorPatientCheckInSettingsMapper.updateByPrimaryKeySelective(doctorPatientCheckInSettings);
    }

    public DoctorPatientCheckInSettings findById(Long id) {
        return doctorPatientCheckInSettingsMapper.selectByPrimaryKey(id);
    }

    public List<DoctorPatientCheckInSettings> findByIds(List<Long> ids) {
        Example example = new Example(DoctorPatientCheckInSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorPatientCheckInSettingsMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorPatientCheckInSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorPatientCheckInSettings doctorPatientCheckInSettings = new DoctorPatientCheckInSettings();
        doctorPatientCheckInSettings.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorPatientCheckInSettingsMapper.updateByExampleSelective(doctorPatientCheckInSettings, example);
    }

}
